#include "menuscene.h"

MenuScene::MenuScene(QWidget *parent)
    : GameScene((QWidget*)parent)
{
    background = new GameBackground(QPixmap(RESOURCE_DIR + "/Kit_MenuBackground.png"),mainScene);
    mainScene->setSceneRect(background->boundingRect());
    mainView->scale(height()/mainScene->height(),height()/mainScene->height());
    mainView->centerOn(background);

    //Setup the menu items in a QList

    playItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_PlayItem_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_PlayItemOver_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_PlayItemSelected_1.png"),
                        mainScene);

    tutorialItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_TutorialItem_1.png"),
                                QPixmap(RESOURCE_DIR + "/menuImages/Kit_TutorialItemOver.png"),
                                QPixmap(RESOURCE_DIR + "/menuImages/Kit_TutorialItemSelected.png"),
                                mainScene);

    helpItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItem_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItemOver_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_HelpItemSelected_1.png"),
                        mainScene);

    quitItem = new MenuItem(QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItem_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItemOver_1.png"),
                        QPixmap(RESOURCE_DIR + "/menuImages/Kit_QuitItemSelected_1.png"),
                        mainScene);

    menu.append(playItem);
    menu.append(tutorialItem);
    menu.append(helpItem);
    menu.append(quitItem);

    for (int index = 0; index < menu.length(); index++)
    {
    menu.at(index)->setPos((mainScene->width()/2)-(menu.at(index)->pixmap().width()/2),
                       (mainScene->height()/2)+((menu.at(index)->pixmap().height()+14)*index)-((menu.length()/2)*(menu.at(index)->pixmap().height()+14))+60);

    QObject::connect(menu.at(index),SIGNAL(clicked(MenuItem*)),this,SLOT(itemClicked(MenuItem*)));
    }
}

void MenuScene::start()
{
    // Reset the menu
    for (int index = 0; index < menu.length();index++)
        menu.at(index)->setActive(false);

    menuPosition = 0;
    menu.at(0)->setActive(true);

    setFocus(Qt::OtherFocusReason);
    grabKeyboard();
}

void MenuScene::endScene(GameScene::GameScenes nextScene)
{
    releaseKeyboard();
    GameScene::endScene(nextScene);
}

void MenuScene::nextScene()
{
    if (menuPosition == 0)
        endScene(levelScene1);

    else if (menuPosition == 1)
        endScene(levelSceneTutorial);

    else if (menuPosition == 2)
        endScene(helpScene);

    else if (menuPosition == 3)
        endScene(endGame);
}

void MenuScene::keyPressEvent(QKeyEvent * key)
{
    // Catch movement keys
    if ((key->key() == Qt::Key_Down) && (menuPosition < 3))
    {
        menu.at(menuPosition)->setActive(false);
        menuPosition++;
        menu.at(menuPosition)->setActive(true);
    }
    else if ((key->key() == Qt::Key_Up) && (menuPosition > 0))
    {
        menu.at(menuPosition)->setActive(false);
        menuPosition--;
        menu.at(menuPosition)->setActive(true);
    }

    // catch the enter key
    else if ((key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return))
    {
        nextScene();
    }
}

void MenuScene::itemClicked(MenuItem *item)
{
    menuPosition = menu.indexOf(item);
    nextScene();
}
